<template>
  <Modal v-model="modal" title="批量生成卡密" @on-ok="ok" @on-cancel="cancel">
    <Form ref="form" :model="formItem" inline :label-width="70">
      <FormItem prop="name" label="卡密名称">
        <Input v-model="formItem.name" placeholder="卡密名称" style="width: 150px" />
      </FormItem>
      <FormItem prop="count" label="数量">
        <InputNumber
          :max="100000"
          :min="1"
          v-model="formItem.count"
          placeholder="数量"
          style="width: 150px"
        />
      </FormItem>
      <FormItem prop="quota" label="额度">
        <InputNumber
          :max="100000"
          :min="1"
          v-model="formItem.quota"
          placeholder="额度"
          style="width: 150px"
        />
      </FormItem>
      <FormItem prop="type" label="时效">
        <Select v-model="formItem.type" placeholder="时效" style="width: 150px">
          <Option :value="-1" :key="-1">年</Option>
          <Option :value="0" :key="0">月</Option>
          <Option :value="1" :key="1">日</Option>
          <Option :value="2" :key="2">小时</Option>
          <Option :value="3" :key="3">分钟</Option>
        </Select>
      </FormItem>
    </Form>
  </Modal>
</template>
<script setup>
import http from '@/util/http'
let { proxy } = getCurrentInstance()
const emit = defineEmits(['refresh'])
let modal = ref(false)
let formItem = reactive({
  name: '',
  count: 1,
  type: -1,
  quota: 1,
})

const modal_show = () => {
  modal.value = true
}

defineExpose({
  modal_show,
})
let type_name = '年'
watch(
  () => formItem.type,
  (val) => {
    switch (val) {
      case -1:
        type_name = '年'
        break
      case 0:
        type_name = '月'
        break
      case 1:
        type_name = '日'
        break
      case 2:
        type_name = '小时'
        break
      case 3:
        type_name = '分钟'
        break
      default:
        type_name = '年'
        break
    }
  }
)

const ok = () => {
  proxy.$Modal.confirm({
    title: '询问',
    content: `将要生成${formItem.count}个，时效为${formItem.quota}${type_name}，名称为${formItem.name}的卡密,是否确认？`,
    onOk: () => {
      http.post('/Basic/SecretKey/BatchCreate', toRaw(formItem), (response) => {
        if (response.data.flag) {
          proxy.$Message.success(response.data.msg)
          emit('refresh')
        } else {
          proxy.$Message.error(response.data.msg)
        }
      })
    },
    onCancel: () => {},
  })
}
</script>